<template>
    <div class="default-main ba-table-box loading">
        <el-alert class="ba-table-alert" v-if="baTable.table.remark" :title="baTable.table.remark" type="info" show-icon />

        <!-- 表格顶部菜单 -->
        <TableHeader
            :buttons="['refresh', 'delete', 'comSearch', 'quickSearch', 'columnDisplay']"
            quick-search-placeholder="快速搜索 操作内容"
        >
            <template #create_time>
                <!-- 在插槽内，您可以随意发挥，通常渲染一个输入框供用户输入内容 -->
                <!-- 输入组件的 v-model="baTable.comSearch.form[item.prop!]" 即可在baTable上下文获取用户输入的关键词 -->
                    <div class="com-search-col-input-range w83">
                        <el-date-picker
                            class="datetime-picker"
                            v-model="baTable.comSearch.form['system_log.create_time']"
                            type="datetimerange"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            value-format="YYYY-MM-DD HH:mm:ss"
                            :teleported="false"
                        />
                    </div>
            </template>
        </TableHeader>
        <!-- 表格 -->
        <!-- 要使用`el-table`组件原有的属性，直接加在Table标签上即可 -->
        <Table />

        <Info />
    </div>
</template>

<script setup lang="ts">
import { concat, cloneDeep } from 'lodash-es'
import { provide } from 'vue'
import baTableClass from '/@/utils/baTable'
import Table from '/@/components/table/index.vue'
import TableHeader from '/@/components/table/header/index.vue'
import { defaultOptButtons } from '/@/components/table'
import { baTableApi } from '/@/api/common'
import Info from './info.vue'
import { buildJsonToElTreeData } from '/@/utils/common'

defineOptions({
    name: 'auth/adminLog',
})


let optButtons: OptButton[] = [
    {
        render: 'tipButton',
        name: 'info',
        title: 'Info',
        text: '',
        type: 'primary',
        icon: 'fa fa-search-plus',
        class: 'table-row-edit',
        disabledTip: false,
        click: (row: TableRow) => {
            infoButtonClick(row)
        },
    },
]

optButtons = concat(optButtons, defaultOptButtons(['delete']))

const baTable = new baTableClass(
    new baTableApi('/admin_api/system_log/'),
    {
        column: [
            { type: 'selection', align: 'center', operator: false },
            { label: 'ID', prop: 'id', align: 'center', operator: false, width: 70 },
            {
                label: '管理员用户名',
                prop: 'username',
                align: 'center',
                operator: false,
                width: 160,
            },
            {
                label: '管理员姓名',
                prop: 'name',
                align: 'center',
                operator: false,
                width: 160,
            },
            {
                label: '管理员手机号',
                prop: 'mobile',
                align: 'center',
                operator: false,
                width: 160,
            },
            { label: '操作内容', prop: 'title', align: 'center', operator: 'LIKE', operatorPlaceholder: '模糊查询' },
            {
                show: false,
                label: '数据',
                prop: 'data',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                showOverflowTooltip: true,
            },
            {
                label: '操作路径',
                prop: 'url',
                align: 'center',
                operator: 'LIKE',
                operatorPlaceholder: '模糊查询',
                showOverflowTooltip: true,
            },
            { label:'操作IP', prop: 'ip', align: 'center', operator: 'LIKE', operatorPlaceholder: '模糊查询', render: 'tag' },

            {
                label: '创建时间',
                comSearchRender:'slot',
                comSearchSlotName: 'create_time',
                prop: 'create_time',
                align: 'center',
                render: 'datetime',
                sortable: 'custom',
                operator: 'RANGE',
                width: 160,
            },
            {
                label: '操作',
                align: 'center',
                width: '100',
                render: 'buttons',
                buttons: optButtons,
                operator: false,
            },
        ],
        dblClickNotEditColumn: [undefined],
    },
    {},
    {
        onTableDblclick: ({ row }) => {
            infoButtonClick(row)
            return false
        },
    }
)

baTable.mount()
baTable.getIndex()

provide('baTable', baTable)

/** 点击查看详情按钮响应 */
const infoButtonClick = (row: TableRow) => {
    if (!row) return
    // 数据来自表格数据,未重新请求api,深克隆,不然可能会影响表格
    let rowClone = cloneDeep(row)
    rowClone.data = rowClone.data ? [{ label: '点击展开', children: buildJsonToElTreeData(JSON.parse(rowClone.data)) }] : []
    baTable.form.extend!['info'] = rowClone
    baTable.form.operate = 'Info'
}
</script>

<style scoped lang="scss"></style>
